﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="Telerik.Web.UI.Common.Core-vsdoc.js" assembly="Telerik.Web.UI" />
/// <reference path="~/Clients/Scripts/jQuery/jquery-1.6.1-vsdoc.js"/>
/// <reference path="~/Clients/Scripts/globals.debug.js"/>
/// <reference path="~/Clients/Scripts/utils.debug.js"/>
/// <reference path="~/Clients/Scripts/common.debug.js"/>
/// <reference path="~/Clients/Scripts/jqueryLookup.debug.js"/>

Type.registerNamespace("GreeNova.LoadingPanels");

GreeNova.LoadingPanelInfo = function (owner, dialog, dialogupdate, dialogclose, testVisible, zIndex) {
    this._owner = owner;
    this.testVisible = testVisible;
    this.dialog = dialog;
    this.dialogupdate = dialogupdate;
    this.dialogclose = dialogclose;
    this.zIndex = zIndex;
};

GreeNova.LoadingPanelInfo.prototype = {
    update: function () {
        this._owner.update();
    },

    hide: function () {
        this._owner.hideLPI(this);
    },

    _shouldBeShown: function () {
        if (this.testVisible) {
            var visible = this.testVisible();
            if (!visible)
                return false;
        }
        return true;
    },

    _doShow: function () {
        var lp = this._owner.lp;
        var target = this._owner.target;

        lp.show(target);

        var e = $get(target + "_wrapper");
        if ((typeof (e) == "undefined") || (!e))
            e = $get(target);
        var f = lp.get_element();
        var d = $("#" + f.id + target)[0];

        return d;
    },

    _doHide: function () {
        this._owner.lp.hide(this._owner.target);
        if (this.dialog && this.dialogupdate) {
            if (this.dialogupdate)
                this.dialog.unbind('drag dragstop resize resizestop', this.dialogupdate);
            if (this.dialogclose)
                this.dialog.unbind('dialogclose', this.dialogclose);
        }
    }
};


GreeNova.LoadingPanelTargetInfo = function (owner, lp, target) {
    this._owner = owner;
    this.target = target;
    this.lp = lp;
    this._lpis = [];
    this._shownLPi = undefined;
    this._shownLPLayer = undefined;
};

GreeNova.LoadingPanelTargetInfo.prototype = {
    show: function (dialog, dialogupdate, dialogclose, testVisible, zIndex) {
        var lpi = new GreeNova.LoadingPanelInfo(this, dialog, dialogupdate, dialogclose, testVisible, zIndex);
        this._lpis.push(lpi);
        this.update();
        return lpi;
    },

    hideLPI: function (loadingPanelInfo) {
        var p = jQuery.inArray(loadingPanelInfo, this._lpis);
        if (p !== -1) {
            var lpi = this._lpis[p];
            this._lpis.splice(p, 1);

            if (lpi === this._shownLPi)
                this.update(lpi);
        }
    },

    hideCompletly: function () {
        this._lpis.length = 0;
        this.update(this._shownLPi);
    },

    hide: function () {
        if (this._lpis.length > 0) {
            var lpi = this._lpis[this._lpis.length - 1];
            this._lpis.length = this._lpis.length - 1;
            if (lpi === this._shownLPi)
                this.update(lpi);
        }
    },

    update: function (removedLPi) {
        if (this._lpis.length === 0) {
            if (removedLPi)
                removedLPi._doHide();
            this._owner._doRemove(this);
            return;
        }

        var sLpi;
        var sZIndex;

        for (var i = 0; i < this._lpis.length; i++) {
            var lpi = this._lpis[i];
            if (lpi._shouldBeShown()) {
                if (!sLpi || lpi.zIndex > sZIndex) {
                    sLpi = lpi;
                    sZIndex = lpi.zIndex;
                }
            }
        }
        if (!sLpi) {
            if (this._shownLPi)
                this._shownLPi._doHide();
            this._shownLPi = undefined;
            this._shownLPLayer = undefined;
        }
        else if (!this._shownLPi) {
            this._shownLPi = sLpi;
            this._shownLPLayer = sLpi._doShow();
            this._doUpdateLayer();
        }
        else if (sLpi !== this._shownLPi) {
            this._shownLPi = sLpi;
            this._doUpdateLayer();
        }
        else {
            this._doUpdateLayer();
        }
    },

    _doUpdateLayer: function () {
        var lp = this.lp;
        var target = this.target;

        var e = $get(target + "_wrapper");
        if ((typeof (e) == "undefined") || (!e))
            e = $get(target);
        var r = lp.getElementRectangle(e);
        var d = this._shownLPLayer;
        d.style.position = "absolute";
        d.style.width = r.width + "px";
        d.style.height = r.height + "px";
        d.style.left = r.left + "px";
        d.style.top = r.top + "px";
        d.style.textAlign = "center";
        d.style.zIndex = this._shownLPi.zIndex;
    },

    _update: function (event, ui) {
        this.update();
    }
};


GreeNova.LoadingPanels = new function () {
    this._tis = [];

    this._getTIIndex = function (target) {
        for (var i = 0; i < this._tis.length; i++) {
            if (this._tis[i].target === target)
                return i;
        }
        return -1;
    };

    this._getTI = function (target) {
        var i = this._getTIIndex(target);
        if (i !== -1)
            return this._tis[i];
    };

    this.show = function (lp, target, testVisible, zIndex) {
        ///	<returns type="GreeNova.LoadingPanelTargetInfo" />
        return this._show(lp, undefined, target, testVisible, zIndex);
    };

    this.showOverDialog = function (lp, dialog, target, testVisible, zIndex) {
        ///	<returns type="GreeNova.LoadingPanelTargetInfo" />
        return this._show(lp, dialog, target, testVisible, zIndex);
    };

    this._getElId = function (el) {
        if (!el)
            return undefined;
        if (GreeNova.JqueryLookup.isInstanceOfType(el))
            el = el.jq();
        if (typeof (el) !== "string" && el.length && el.length > 0)
            el = el[0];
        if (el.id)
            el = el.id;
        return el;
    };

    this._show = function (lp, dialog, target, testVisible, zIndex) {
        if (!lp)
            throw "Loading panel is not defined";

        if (GreeNova.JqueryLookup.isInstanceOfType(lp))
            lp = lp.component();
        else if (typeof lp === "string") {
            lp = $find(lp);
            if (lp === null)
                throw "Loading panel with ID: [" + lp + "] was not found";
        }
        if (!lp)
            throw "Loading panel is not defined";
        if (lp._isSticky)
            throw "Sticky Loading panel is not supported";

        target = this._getElId(target);
        if (!target || typeof (target) !== "string")
            throw "target is not deffined or not string";

        var d;
        if (dialog) {
            if (GreeNova.JqueryLookup.isInstanceOfType(dialog))
                dialog = dialog.jq();
            d = dialog.parent();
        }

        var ti = this._getTI(target);
        if (!ti) {
            ti = new GreeNova.LoadingPanelTargetInfo(this, lp, target);
            this._tis.push(ti);
        }
        var du;
        var dc;
        if (d) {
            du = ti._update.setCtx(ti);
            d.bind('drag dragstop resize resizestop', du);
            dc = ti.hideCompletly.setCtx(ti);
            d.bind('dialogclose', dc);
        }
        return ti.show(d, du, dc, testVisible, zIndex || lp._zIndex);
    };

    this.update = function (target) {
        target = this._getElId(target);
        if (!target) {
            for (var i = 0; i < this._tis.length; i++)
                this._tis[i].update();
        }
        else {
            var ti = this._getTI(target);
            if (ti)
                ti.update();
        }
    };

    this.hideCompletly = function (target) {
        target = this._getElId(target);
        if (!target || typeof (target) !== "string")
            throw "target is not deffined or not string";

        var ti = this._getTI(target);
        if (ti)
            ti.hideCompletly();
    };

    this.hide = function (target) {
        target = this._getElId(target);
        if (!target || typeof (target) !== "string")
            throw "target is not deffined or not string";

        var ti = this._getTI(target);
        if (ti)
            ti.hide();
    };

    this._doRemove = function (targetInfo) {
        var p = jQuery.inArray(targetInfo, this._tis);
        if (p !== -1)
            this._tis.splice(p, 1);
    }
};
